<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入避难点地址" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-input style="width: 200px" placeholder="请输入条件" suffix-icon="el-icon-message" class="ml-5" v-model="content"></el-input>
<!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="source"></el-input>-->
      <el-select v-model="source" filterable placeholder="请选择来源" class="ml-5">
        <el-option
            v-for="item in sourceOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-select v-model="type" filterable placeholder="请选择类型" class="ml-5">
        <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-upload action="http://localhost:9090/refuges/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'header'"  :header-cell-style="{textAlign: 'center'}" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <!--      <el-table-column fixed="left" prop="id" label="ID" width="80" sortable></el-table-column>-->
      <el-table-column
          type="index"
          width="50">
      </el-table-column>
      <el-table-column prop="name" label="避难点地址" sortable width="120" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column label="图片" width="100"><template slot-scope="scope"><el-image style="width: 100px; height: 100px" :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image></template></el-table-column>
      <el-table-column prop="phone" label="联系方式" width="100"></el-table-column>
      <el-table-column prop="contacts" label="联系人" width="80"></el-table-column>
      <el-table-column prop="content" label="条件" sortable :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="source" label="来源" sortable width="80" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.source">官方</el-tag>
          <el-tag type="" v-else>民间</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" sortable width="80" align="center">
        <template slot-scope="scope" >
          <el-tag effect="dark" type="success" v-if="scope.row.type">未满员</el-tag>
          <el-tag effect="dark" type="danger" v-else>已满员</el-tag>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作"  width="280" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="避难点地址">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload action="http://localhost:9090/file/upload" ref="img" :on-success="handleImgUploadSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="form.contacts" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="条件">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <!--        <el-form-item label="来源">-->
        <!--          <el-input v-model="form.source" autocomplete="off"></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="来源" prop="source">
          <el-select v-model="sourceForm" placeholder="请选择来源">
            <el-option label="官方" value="1"></el-option>
            <el-option label="民间" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="类型">
          <el-select v-model="typeForm" placeholder="请选择类型">
            <el-option label="未满员" value="1"></el-option>
            <el-option label="已满员" value="0"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import jwtDecode from "jwt-decode";

export default {
  name: "Refuges",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      content: "",
      source: null,
      type: null,
      sourceOptions: [{value: '1', label: '官方'}, {value: '0', label: '民间'}],
      typeOptions: [{value: '1', label: '未满员'}, {value: '0', label: '已满员'}],

      form: {},
      sourceForm: "",
      typeForm: "",
      rules:{
        source:[
          {required: true, message: '请选择来源'}
        ]
      },
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/refuges/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          content: this.content,
          source: this.source,
          type: this.type
        }
      }).then(res => {
        console.log("来源：", this.source);

        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    save() {
      console.log("save_form1:", this.form);
      console.log("save_form_source:", this.form.source);
      console.log("save_form_sourceForm:", this.sourceForm);

      // let type_source = Object.prototype.toString.call(this.sourceForm);
      // let type_type = Object.prototype.toString.call(this.typeForm);
      // console.log("type_source:", type_source);
      // console.log("type_type:", type_type);

      if (this.sourceForm == 1 || this.sourceForm == 0){
        this.form.source = this.sourceForm
      }
      if (this.typeForm == 1 || this.typeForm == 0){
        this.form.type = this.typeForm
      }

      const code = jwtDecode(this.user.token)
      this.form.userId = code.aud
      this.request.post("/refuges", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      if (this.form.source == 1){
        this.sourceForm = "官方"
      }else {
        this.sourceForm = "民间"
      }
      if (this.form.type == 1){
        this.typeForm = "未满员"
      }else {
        this.typeForm = "已满员"
      }
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if(this.$refs.img) {
          this.$refs.img.clearFiles();
        }
        if(this.$refs.file) {
          this.$refs.file.clearFiles();
        }
      })
    },
    del(id) {
      this.request.delete("/refuges/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/refuges/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.content = ""
      this.source = null
      this.type = null

      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    download(url) {
      window.open(url)
    },
    exp() {
      window.open("http://localhost:9090/refuges/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>


<style>
/*.headerBg {*/
/*  background: #eee!important;*/
/*}*/
.header{
  /*background: #eee!important;*/
  color: rgb(102, 102, 102);
  font-size: 14px;
}
.el-tooltip__popper{
  max-width:20%;
}
.el-tooltip__popper,.el-tooltip__popper.is-dark{
  background:rgb(48, 65, 86) !important;
  color: #fff !important;
  line-height: 24px;
}
</style>
